/*  This program sort two parallel arreys
       Written by: Amrita mukherjee
       Date: 19th June 2010

*/

#include <stdio.h>
#include <stdlib.h>

//function declaration
void header (void);
void selectionSort_descending(int* stud_ID, int* mid_score, int last);
void footer (void);

int main (void)
{
//  Local Declarations
    FILE* inputData;
    FILE* outputData;
    int student_ID;
    int midterm_score;
    int stud_ID[30];
    int mid_score[30];
    int last;
    int current;

//  Statements
    header ();

    if (!(inputData = fopen("AG_Spring.txt", "r")))
           {
            printf("Error opening text.txt for reading\n");
            exit (100);
           } // if open error
    last = 0;
    while (fscanf(inputData, "%d    %d\n", &student_ID, &midterm_score) != EOF)
        {
        stud_ID[last] = student_ID;
        mid_score[last] = midterm_score;
        last++;
        }
    selectionSort_descending( stud_ID, mid_score, last);
    outputData = fopen("Sorted.txt", "w");
    printf("ID  Midterm Score\n");
    fprintf(outputData, "This program sorts midterm score in decending order\nwith corresponding student IDs\n\n\n");

    fprintf(outputData, "ID Midterm Score\n");

    for (current = 0; current<last; current++)
        {
        printf("%d    %d\n", stud_ID[current], mid_score[current]);
        fprintf(outputData, "%d    %d\n", stud_ID[current], mid_score[current]);
        }
    fprintf(outputData,"\n\nEnd of sorted midterm report\n");
    fclose(inputData);
    fclose(outputData);

    footer ();

    return 0;
}




/*  ==================== header ====================
    This function prints welcome message and information about the program.
       Pre    Nothing
       Post   header printed
*/
void header ()
{
//  Local Declarations

//  Statements
    printf("This program sorts midterm score in decending order\nwith corresponding student IDs\n\n\n");


    return;
}   // header

/*==================== selsort_descending ====================
This function sorts in decending order

pre: student id, midterm score
post: midterm score sorted in decending order
*/

void selectionSort_descending(int* stud_ID , int* mid_score, int last)
{
    int largest;
    int tmp;
    int current, walk;


    for (current = 0; current < last-2; current++)
        {
        largest = current;
        for (walk = current; walk <= last-1; walk++)
            {
            if (mid_score[walk] > mid_score[largest])
                {
                largest = walk;
                }
            }

        tmp = mid_score[current];
        mid_score[current] = mid_score[largest];
        mid_score[largest] = tmp;

        tmp = stud_ID[current];
        stud_ID[current] = stud_ID[largest];
        stud_ID[largest] = tmp;
        }

    return;
} // selectionSort_descending

/*  ==================== footer ====================
    This function prints end og program msg.
       Pre    Nothing
       Post   foooter printed
*/
void footer ()
{
//  Local Declarations

//  Statements
    printf("\n\nEnd of sorted midterm report\n");
    

    return;
}   // footer







/* results :
This program sorts midterm score in decending order
with corresponding student IDs


ID      Midterm Score
6188    96
7193    96
5809    95
6786    95
3456    90
9004    88
4567    86
1001    86
8008    86
4308    84
6008    84
7448    84
4810    83
7880    83
3199    82
4009    81
1919    81
2092    80
1419    79
6098    77
7010    72
8709    72
4770    70
1094    70
4098    64
9051    30


End of sorted midterm report
Press any key to continue



*/


